<template>
  <j-form-container :disabled="disabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="实付金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['amountPaid', validatorRules.amountPaid]" placeholder="请输入实付金额" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="应付金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['amountPayable', validatorRules.amountPayable]" placeholder="请输入应付金额" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="销售单创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择销售单创建日期" v-decorator="['saleDate']" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['state']" :trigger-change="true" dictCode="order_state" placeholder="请选择状态"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['remarks']" placeholder="请输入备注"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="客户" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select v-decorator="['customerName']" placeholder='请选择客户'>
                                <a-select-option v-for='temp in customerList' :key='temp.customerId'>
                                  {{ temp.customerName }}
                                </a-select-option>
                              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
  </j-form-container>
</template>
<script>
  import pick from 'lodash.pick'
  import { getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'SaleListForm',
    components: {
      JFormContainer,
      JDate,
      JDictSelectTag,
    },
    props:{
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        customerList:[],
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        validatorRules: {
          amountPaid: {
            rules: [
              { required: true, message: '请输入实付金额!'},
            ]
          },
          amountPayable: {
            rules: [
              { required: true, message: '请输入应付金额!'},
            ]
          },
        },
        confirmLoading: false,
      }
    },
    methods:{
      initFormData(url,id){
        this.clearFormData()
        if(!id){
          this.edit({})
        }else{
          getAction(url,{id:id}).then(res=>{
            if(res.success){
              let records = res.result
              if(records && records.length>0){
                this.edit(records[0])
              }
            }
          })
        }
      },
      edit(record){
        this.model = Object.assign({}, record)
        console.log("SaleListForm-edit",this.model);
        let fieldval = pick(this.model,'amountPaid','amountPayable','saleDate','state','remarks','customerName')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
      },
      getFormData(){
        let formdata_arr = []
        this.form.validateFields((err, values) => {
          if (!err) {
            //let this.model = omit(this.model,'customerName')
            console.log("model:",this.model);
            let formdata = Object.assign(this.model, values)
            let isNullObj = true
            Object.keys(formdata).forEach(key=>{
              if(formdata[key]){
                isNullObj = false
              }
            })
            if(!isNullObj){
              formdata_arr.push(formdata)
            }
          }else{
            this.$emit("validateError","tb_sale_list表单校验未通过");
          }
        })
        console.log("tb_sale_list表单数据集",formdata_arr);
        return formdata_arr;
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'amountPaid','amountPayable','saleDate','state','remarks','customerName'))
      },
      clearFormData(){
        this.form.resetFields()
        this.model={}
      },
      saveCustomerList() {
            getAction("sale/saleListGoods/getCustomerList").then((resp)=>{
              if (resp.success){
                this.customerList=resp.result;
              }
            })
          }
    },
    created() {
      this.saveCustomerList();
    }
  }
</script>
